[WIP/do not merge] Replace anyhow with typed errors in wit-parser#2460
[WIP/do not merge] Replace anyhow with typed errors in wit-parser#2460PhoebeSzmucer wants to merge 2 commits intobytecodealliance:mainfrom
anyhow with typed errors in wit-parser#2460Conversation
|
Thanks for this! I mentioned this on Zulip but wanted to echo here too -- I agree with the direction of this change and I think it would be good to have concrete error types for
Overall I'd say let's figure out the final design of the error types in play here, and once that's sorted out figure out how best to land this (e.g. all-at-once vs incrementally) -- does that sound ok? |
|
Thank you for a very detailed response @alexcrichton !
Totally agreed. I'll try to merge some non-controversial pieces first.
To clarify - we'd just make
For a very basic LSP we don't need it, but I think it would be a good idea to be more fine-grained than just span+message. It's the difference between the consumers (the LSP, or even the CLI) knowing just what to highlight and what to say, vs these tools actually understanding what the error is. Typed variants unlock richer things -
I agree that the Span is the right internal primitive, but it raises the question for the public API - the consumer needs to know which source map a given
This is manageable but leaks internal architecture to callers. In case of An alternative is for errors to carry already-resolved locations. Not pre-rendered strings, but structured I'm happy to keep exposing global spans in the API, but for that I might need to modify how package cycles are computed - from my understanding cycles are detected during toposort before source maps are merged, which is why I did eager resolution in my PR. I think if we merge the source maps before toposort that would work. |
Motivation
Today, getting a file path and line number out of a parse error requires parsing the human-readable error string that anyhow produces. This is fragile: it breaks silently when error message formatting changes, and it's simply the wrong abstraction.
I want to use
wit-parserin an LSP for WIT/WAC, but this change would be good for any tool that embedswit-parserand wants to do something useful with errors.I Know This Is Large
I'm aware this PR is bigger than ideal. The core reason it's large is that error type changes propagate through the entire call stack - there's no natural seam to split at. If this is something that we'd like to merge in some form, I can try splitting up this PR into smaller chunks - I wanted to open this draft first to start a conversation.
Global vs local spans
Currently I'm returning global
Spanobjects as part ofWitError, because existing code uses global spans almost exclusively. This makes things annoying for the consumer - they have to use theSourceMapobject to map to file-local offsets. I'm considering changing the exposed spans to be resolved local spans.